from sklearn.model_selection import GridSearchCV
from sklearn.neural_network import MLPClassifier
from sklearn.datasets import load_iris

# 加载数据集
iris = load_iris()
X, y = iris.data, iris.target

# 定义参数网格
param_grid = {
    'hidden_layer_sizes': [(50,), (100,), (50, 50), (100, 100)],
    'activation': ['tanh', 'relu'],
    'alpha': [0.0001, 0.001, 0.01]
}

# 创建MLPClassifier实例
mlp = MLPClassifier(max_iter=1000)

# 创建GridSearchCV实例
grid_search = GridSearchCV(estimator=mlp, param_grid=param_grid, cv=5, scoring='accuracy')

# 执行搜索
grid_search.fit(X, y)

# 输出最佳参数和最佳分数
print("Best parameters: ", grid_search.best_params_)
print("Best score: ", grid_search.best_score_)
